<?php
namespace hyang\db;
// 定义子命名空间 
/*
*  数据库信息查询-引擎
*/
class Database{
    public $option;// 数据库选项
    public $db;//数据库操作类
    public $model;//数据库模式对象    
    public $valid;// 对象是否合法
    public $info;// 类调用信息
    private $linkParam;//链式参数
    public function __construct($opt=null)
    {
        $ctt = 0;
        $cttAll = 2;
        if(isset($opt['db']) && is_object($opt['db'])){$this->db = $opt['db'];$ctt += 1;}
        if(isset($opt['option']) && is_array($opt['option'])){$this->option = $opt['option'];}
        if(isset($opt['model']) && is_object($opt['model'])){$this->model = $opt['model'];$ctt += 1;}
        $this->valid = $ctt == $cttAll? true:false;
        if($this->valid == false) $this->info = 'Database 对象初始失败';
        else $this->info = 'Database 已经成功初始化';
    }
    // 全部数据查询语句
    private function _selectSQL($key=null)
    {
        $type = $this->db->type;
        $opt = $this->model->getOpt();
        $sql = '';
        switch($type){
            case 'MYSQL':// 系统数据表： mysql/information_schema/performance_schema
                if($key == 'count') $sql = 'select count(*) from information_schema.tables where table_schema="'.$opt['dbname'].'"';
                if($key == 'view_count') $sql = 'select count(*) from information_schema.tables where table_schema="'.$opt['dbname'].'"';
                elseif($key == 'database') $sql = 'show databases';
                elseif($key == 'view') $sql = 'select * from information_schema.views where table_schema="'.$opt['dbname'].'"';
                else $sql = 'select * from information_schema.tables where table_schema="'.$opt['dbname'].'"';
                break;
            case 'ORACLE':
                if($key == 'count') $sql = 'select count(*) from all_tables WHERE owner=\''.$opt['user'].'\'';
                else $sql = 'select * from all_tables WHERE owner=\''.$opt['user'].'\'';
                break;
            case 'PGSQL':
                if($key == 'count') $sql = 'select count(*) from pg_class where relnamespace=2200';
                break;
        }
        return $sql;
    }
    // 获取全部是数据表
    public function table()
    {
        if($this->valid == false) return false;
        $sql = $this->_selectSQL();
        if(empty($sql)) return null;
        return $this->db->query($sql);
    }
    // 获取全部的视图
    public function view()
    {
        if($this->valid == false) return false;
        $sql = $this->_selectSQL(__FUNCTION__);
        if(empty($sql)) return null;
        return $this->db->query($sql);
    }
    // 获取全部的视图
    public function view_count()
    {
        if($this->valid == false) return false;
        $sql = $this->_selectSQL(__FUNCTION__);
        if(empty($sql)) return null;
        return $this->db->query($sql);
    }
    // 获取当前总表格数
    public function count()
    {
        if($this->valid == false) return false;
        $sql = $this->_selectSQL(__FUNCTION__);
        if(empty($sql)) return null;
        return $this->db->fetchOne($sql);
    }
    // 显示所有数据库
    public function database()
    {
        if($this->valid == false) return false;
        $sql = $this->_selectSQL(__FUNCTION__);
        if(empty($sql)) return null;
        return $this->db->query($sql);
    }
}